package sm.utils;

import com.vmware.vim25.DynamicProperty;
import com.vmware.vim25.InvalidPowerState;
import com.vmware.vim25.ObjectContent;
import com.vmware.vim25.RetrieveResult;
import com.vmware.vim25.VimPortType;

public class ServerUtils {
	public static String[] congeString(String[] a, String[] b){
		int length = 0;
		length = a.length + b.length;
		String[] result = new String[length];
		int curPosition = 0;
		for(int i=0;i<a.length;i++){
			result[curPosition] = a[i];
			curPosition++;
		}
		for(int i=0;i<b.length;i++){
			result[curPosition] = b[i];
			curPosition++;
		}
		
		return result;
	}
	
	public static void VmOnOffControl(VimPortType _service,RetrieveResult props,
			String vmName,String operation){
	//	ManagedObjectReference taskmor = null;
		String key, value;
		for (ObjectContent oc:props.getObjects()){
			for (DynamicProperty dp:oc.getPropSet()){
				key=dp.getName();
				value=dp.getVal().toString();
				if (key.equals("name") && value.equals(vmName)){
					try{	
						if(PropertySet._powerOn.equals(operation)){
							System.out.println("Powering ON virtualmachine '"+value+"'");
							_service.powerOnVM_Task(oc.getObj(), null);
						}
						else if(PropertySet._powerOff.equals(operation)){
							System.out.println("Powering OFF virtualmachine '"+value+"'");
							_service.powerOffVM_Task(oc.getObj());
						}
					} catch(InvalidPowerState e){
						System.out.println("Virtual Machine is already powered on");
					} catch(Exception e){
						System.out.println("Error");
						e.printStackTrace();
					}
				}
			}
		}
	}
}
